var now = 1;

function init(){
	//알림마당 초기화
	var list_zone = $("dl.notice dd.list_zone");
	view(list_zone[0]);
	changeSearchBunEvent();
	changeListEvent();

	//학사안내 초기화
	$("#academics_univ").css("font-weight", "bold");
	$("#academics_univ").css("background-color", "#8ACF57");
	$("#academics_univ").css("color", "#ffffff");
	
	$("#academics_graduate").css("font-weight", "normal");
	$("#academics_graduate").css("background-color", "#ffffff");
	$("#academics_graduate").css("color", "#5e5e5e");
	
	$("#academics_content_univ").show();
	$("#academics_content_graduate").hide();
	
	changeAcademics();
	gbRefresh();
	gbPagging();
	gbSubmit();
	gbDelete();
} 

/**
 * 알림마당
 */
function changeListEvent(){
	var univ = $("dl.notice span.univ");
	var graduate = $("dl.notice span.graduate");
	
	for(var i=0; i<univ.length; i++){
		univ[i].onclick = function(){
			allGraduateHide();
			$("#" + $(this).attr("id") + "_list").show();
			$(this).css("font-weight", "bold");
			$(this).css("background-color", "#8ACF57");
			$(this).css("color", "#ffffff");
		};
		graduate[i].onclick = function(){
			allUnivHide();
			$("#" + $(this).attr("id") + "_list").show();
			$(this).css("font-weight", "bold");
			$(this).css("background-color", "#8ACF57");
			$(this).css("color", "#ffffff");
		};
	}
	
}

//모든 대학공지를 보여준다.
function allUnivShow(){
	$("dl.notice ul.univ_list").show();
	allChangeListZoneBtStyle("graduate");
}

//모든 대학공지를 숨긴다
function allUnivHide(){
	$("dl.notice ul.univ_list").hide();
	allChangeListZoneBtStyle("univ");
}

//모든 대학원 공지를 보여준다.
function allGraduateShow(){
	$("dl.notice ul.graduate_list").show();
	allChangeListZoneBtStyle("univ");
}

//모든 대학원 공지를 숨긴다.
function allGraduateHide(){
	$("dl.notice ul.graduate_list").hide();
	allChangeListZoneBtStyle("graduate");
}

function allChangeListZoneBtStyle(type){
	$("dl.notice span." + type).css("font-weight", "normal");
	$("dl.notice span." + type).css("background-color", "#ffffff");
	$("dl.notice span." + type).css("color", "#5e5e5e");
}

//알림마당의 공지사항을 변경
function changeSearchBunEvent(){
	var searchBunPrev = $("dl.notice img.prev")[0];
	var searchBunNext = $("dl.notice img.next")[0];
	
	searchBunPrev.onclick = changeSearchBunPrev;
	searchBunNext.onclick = changeSearchBunNext;
	 
}

//이전 공지사항으로 변경
function changeSearchBunPrev(){
	var now = 1;
	var list_zone = $("dl.notice dd.list_zone");
	
	for(var i=0; i<list_zone.length; i++){
		if($(list_zone[i]).css("display") == "block"){
			now = $(list_zone[i]).attr("id").slice(10,11);
		}
	}
	
	if(now == "1"){
		now = 5;
	}else{
		now = parseInt(now) - 1;
	}
	
	var obj = $("#list_zone_" + now)[0];
	view(obj);
}

//다음 공지사항으로 변경
function changeSearchBunNext(){
	var now = 1;
	var list_zone = $("dl.notice dd.list_zone");
	
	for(var i=0; i<list_zone.length; i++){
		if($(list_zone[i]).css("display") == "block"){
			now = $(list_zone[i]).attr("id").slice(10,11);
		}
	}
	
	if(now == "5"){
		now = 1;
	}else{
		now = parseInt(now) + 1;
	}
	
	var obj = $("#list_zone_" + now)[0];
	view(obj);
}

//모든 알림마당 공지를 숨긴 후 선택된 공지만 보여준다.
function view(obj){
	allHide();
	allGraduateHide();
	allUnivShow();
	$(obj).show();
	changeSearchBunBt(obj, "font-weight", "bold");
	changeSearchBunBt(obj, "background-color", "#8ACF57");
	changeSearchBunBt(obj, "color", "#ffffff");
}

//모든 알림을 숨긴다.
function allHide(){
	$("dl.notice dd.list_zone").hide();
}

//알림마당의 대학, 대학원 버튼의 스타일 지정
function changeSearchBunBt(obj, css, value){
	try{
		$("span.univ", obj).css(css, value);
		//$($($(obj).children()[0]).children()[0]).css(css, value);
	}catch(e){}
}

/**
 * 학사안내
 */
function changeAcademics(){
	
	$("#academics_univ")[0].onclick = function(){
		$("#academics_univ").css("font-weight", "bold");
		$("#academics_univ").css("background-color", "#8ACF57");
		$("#academics_univ").css("color", "#ffffff");
		
		$("#academics_graduate").css("font-weight", "normal");
		$("#academics_graduate").css("background-color", "#ffffff");
		$("#academics_graduate").css("color", "#5e5e5e");
		
		$("#academics_content_univ").show();
		$("#academics_content_graduate").hide();
		$("#calendar_link")[0].setAttribute("href", contextPath + "/university/school_calendar.do");
	}; 
	
	$("#academics_graduate")[0].onclick = function(){
		$("#academics_graduate").css("font-weight", "bold");
		$("#academics_graduate").css("background-color", "#8ACF57");
		$("#academics_graduate").css("color", "#ffffff");
		
		$("#academics_univ").css("font-weight", "normal");
		$("#academics_univ").css("background-color", "#ffffff");
		$("#academics_univ").css("color", "#5e5e5e");
		
		$("#academics_content_graduate").show();
		$("#academics_content_univ").hide();
		$("#calendar_link")[0].setAttribute("href", contextPath + "/graduate/graduate_school_calendar.do");
	}; 
	
}

function gbPagging(){
	$(".c_pagging a").click(function(){
		var url = $.url($(this).attr("href"));
		listUpdate(url.param("gb_page"));
		return false;
	});
}

function gbRefresh(){
	$("dt.c_count a").click(function(){
		listUpdate(1);
		return false;
	});
}

function gbSubmit(){
	$("#c_reg_bt").click(function(){
		var mode = $("#mode").val();
		var writer = $("#writer").val();
		var password = $("#password").val();
		var c_txt = $("#c_txt").val();
		
		var url = contextPath + "/guestbook.do";
		var params = 
			"mode=" + mode + "&" + 
			"writer=" + writer + "&" +
			"password=" + password + "&" +
			"c_txt=" + c_txt;
		
		$.ajax({      
		    type:"POST",
		    data:params,
		    url:url,
		    dataType:"html",
		    success:function(data){
		    	$("#guest_book").remove();
		    	$("#guest_book_wrap").html(data);
		    	gbRefresh();
		    	gbPagging();
		    	gbSubmit();
		    	gbDelete();
		    	return false; 
		    }
		});  
		return false; 
	});
}

function gbDelete(){
	$(".c_content dt .d_bt").click(function(){
		var mode = "d";
		var gb_id = $(this).parent().attr("id"); 
		var gb_pwd = prompt("비밀번호를 입력하세요");

		var url = contextPath + "/guestbook.do";
		var params = 
			"mode=" + mode + "&" + 
			"gb_id=" + gb_id + "&" +
			"gb_pwd=" + gb_pwd;
		if(gb_pwd != null){
			$.ajax({      
			    type:"POST",
			    data:params,
			    url:url,
			    dataType:"html",
			    success:function(data){
			    	$("#guest_book").remove();
			    	$("#guest_book_wrap").html(data);
			    	gbRefresh();
			    	gbPagging();
			    	gbSubmit();
			    	gbDelete();
			    }
			});  
		}
		
		return false;
		
	});
}


function listUpdate(gb_page){
	var url = contextPath + "/guestbook.do";
	var params = "gb_page=" + gb_page; 
	
    $.ajax({      
        type:"POST",
        data:params,
        url:url,
        dataType:"html",
        success:function(data){
        	$("#guest_book").remove();
        	$("#guest_book_wrap").html(data);
        	gbRefresh();
        	gbPagging();
        	gbSubmit();
        	gbDelete(); 
        } 
    });  
}


$(window).load(function() {
	init();
});